// pages/stu/login/login.js
import WxValidate from '../../../utils/WxValidate';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    universityList:[],
    universityIndex:null,

    majorList:[],
    majorIndex:null,

    classList:[],
    classIndex:null,

    stu:{
      stuId:'',
      stuName:''
    },

    error:'',

  },

  universityChange(e){
    if(this.data.universityIndex==e.detail.value){
      return;
    }
    this.setData({
      universityIndex: e.detail.value,
      majorIndex:null,     
      classIndex:null,
      classList:[],
      'stu.recv':this.data.universityList[e.detail.value]
    });

    this.getMajorList();

  },

  majorChange(e){
    if(this.data.majorIndex==e.detail.value){
      return;
    }
    this.setData({
      majorIndex: e.detail.value,
      classIndex:null,
      'stu.major':this.data.majorList[e.detail.value]
    });
    this.getClassList();
  },

  classChange(e){
    this.setData({
      classIndex: e.detail.value,
      'stu.clazz':this.data.classList[e.detail.value]
    });
  },

  getUniversityList(){
    app.get({
      url: '/stu/login/university'
    }).then(res=>{
      this.setData({universityList:res.data});
    })    
  },

  getMajorList(){
    app.get({
      url: '/stu/login/major',
      data:{university:this.data.universityList[this.data.universityIndex]}
    }).then(res=>{
      this.setData({majorList:res.data});
    });
    
  },

  getClassList(){
    app.get({
      url: '/stu/login/class',
      data:{
        university:this.data.universityList[this.data.universityIndex],
        major:this.data.majorList[this.data.majorIndex]
      }
    }).then(res=>{
      this.setData({classList:res.data});
    });
  },

  formInputChange(e) {
    const {field} = e.currentTarget.dataset;
    this.setData({
        [`stu.${field}`]: e.detail.value
    })
  },

  login(){
    

    if (!this.WxValidate.checkForm(this.data.stu)) {
      console.log(this.data.stu);
      const error = this.WxValidate.errorList[0];    
      this.setData({error:error.msg});
      return false;
   }


   wx.login({
     success: (res0) => {
      
      this.data.stu.code=res0.code;
      
      app.post({
        url:'/stu/login',
        data:this.data.stu
      })
      .then(res=>{ 
  
        if(res.cookies && res.cookies.length){
          let cookie = res.cookies[0].split(";")[0].trim();            
          app.globalData["Cookie"]=cookie;
        }
  
        app.globalData.currUser = this.data.stu;//记录当前用户
        
        setTimeout(()=>{
          wx.navigateTo({
            url: '../hw/hw'
          });
        },1500);
            
      })
      .catch(res=>{
        this.setData({error:res.data.message})
      });


     },
   })

    

  },

  initValidate(){
    const rules = {
      recv:{required:true},
      major:{required:true},
      clazz:{required:true},
      stuId:{required:true,xh:true},
      stuName:{required:true,stuName:true}

    };
    const messages = {
      recv:{required:"请选择院校！"},
      major:{required:'请选择专业！'},
      clazz:{required:'请选择班级！'},
      stuId:{required:'请填写学号！',xh:'学号必须为10-11为数字！'},
      stuName:{required:'请填写姓名！',stuName:'姓名必须为2-3个汉字！'}

    };
    this.WxValidate = new WxValidate(rules, messages);

    // 自定义验证规则
    this.WxValidate.addMethod(
      'xh',
      (value, param) => {
        if(this.WxValidate.optional(value)){
          return false;
        }

        if(!/^[0-9]{10,11}$/.test(value)){
          return false;
        }
        return true;
      }, 
      '学号格式错误!'
    );

    this.WxValidate.addMethod(
      'stuName',
      (value, param) => {
        console.log('---',param,'---');
        if(this.WxValidate.optional(value)){
          return false;
        }

        if(!/^[\u4E00-\u9FA5]{2,3}$/.test(value)){
          return false;
        }
        return true;
      }, 
      '姓名必须是2到3个汉字!'
    );
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUniversityList();
    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})